<template>
  <el-dialog v-if="load" title="批量修改供应商信息" v-model="visible" append-to-body :close-on-click-modal="false"
    :z-index="2000" width="800px" @close="handleClose">
    <el-form label-position="top">
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="启用状态">
          <template #label>
            <div class="flex items-center w-full gap-2">
              启用状态
              <!-- <el-checkbox v-model="form.status_push" :true-value="2" :false-value="1" size="small">推送赛盒</el-checkbox> -->
            </div>
          </template>
          <VSelect v-model="form.status" :options="options.status" placeholder="请选择" class="w-full" />
        </el-form-item>
        <el-form-item label="海外仓采购" prop="oversea_purchase_id">
          <VCascader v-model="form.oversea_purchase_id" placeholder="请选择" class="w-full"
            @change="({ labels }) => form.oversea_purchase = labels" />
        </el-form-item>
        <el-form-item label="FBA采购" prop="fba_purchase_id">
          <VCascader v-model="form.fba_purchase_id" placeholder="请选择" class="w-full"
            @change="({ labels }) => form.fba_purchase = labels" />
        </el-form-item>
        <el-form-item label="供应商负责人">
          <VCascader v-model="form.director_id" placeholder="请选择" class="w-full" />
        </el-form-item>
        <el-form-item label="结算方式">
          <template #label>
            <div class="flex items-center w-full gap-2">
              结算方式
              <!-- <el-checkbox v-model="form.period_push" :true-value="2" :false-value="1" size="small">推送赛盒</el-checkbox> -->
            </div>
          </template>
          <VSelect v-model="form.period_id" placeholder="请选择" type="period" class="w-full" />
        </el-form-item>
        <el-form-item label="供应商等级">
          <template #label>
            <div class="flex items-center w-full gap-2">
              供应商等级
              <!-- <el-checkbox v-model="form.supplier_level_push" :true-value="2" :false-value="1" size="small">推送赛盒</el-checkbox> -->
            </div>
          </template>
          <VSelect v-model="form.supplier_level" :options="options.supplier_level" placeholder="请选择" class="w-full" />
        </el-form-item>
      </div>
      <el-form-item label="公司地址" prop="company_area_id" class="col-span-2">
        <Area v-model="form" field="company" />
      </el-form-item>
      <el-form-item label="工厂地址" prop="factory_area_id" class="col-span-2">
        <template #label>
          <div class="flex items-center w-full gap-2">
            工厂地址
            <!-- <el-checkbox v-model="form.factory_address_push" :true-value="2" :false-value="1" size="small">推送赛盒</el-checkbox> -->
          </div>
        </template>
        <Area v-model="form" field="factory" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
import Area from '@/components/Area'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (s_ids) => {
  if (!s_ids?.length) {
    ElMessage.warning('请先选择行!')
    return
  }
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  form.value = {
    s_ids,
    status: null,
    oversea_purchase_id: null,
    fba_purchase_id: null,
    director_id: null,
    period_id: null,
    supplier_level: null,
    company_area_id: [],
    company_area_name: [],
    company_address: '',
    factory_area_id: [],
    factory_area_name: [],
    factory_address: '',
    status_push: 1,
    period_push: 1,
    supplier_level_push: 1,
    factory_address_push: 1,
  }
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  loadings.submit = true
  api.batchEdit(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>